package com.game.server.db.dao;

import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.sql.Types;
import java.util.Random;

public class CallProcess {

	public static int RegisterUser(String acc, String pass, String yzm) {

			 LogManager.getInstance().Log("RegisterUser密码："+pass);
			 String cmdText = "select id from users  where account=?";
			 Parameter p1 = new Parameter(12, acc, ParameterType.Input);
			 Object obj = SqlHelperwrite.execScalar(cmdText, new Parameter[] { p1 });

			 java.util.Date currentDate = new java.util.Date();
			 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String date1 =formatter.format(currentDate);
			 LogManager.getInstance().Log("当前时间："+date1);
	   		 if (obj == null) {
				 String cmdText2 = "insert into users(nickname,account,password,logintime,head)values(?,?,?,?,?)";
				 Random random = new Random(System.currentTimeMillis());
				 random.nextInt();

				 Parameter p10 = new Parameter(12, "用户" + acc.substring(acc.length() - 4), ParameterType.Input);
				 Parameter p11 = new Parameter(12, acc, ParameterType.Input);
				 Parameter p12 = new Parameter(12, pass, ParameterType.Input);
				 Parameter p13 = new Parameter(12, date1, ParameterType.Input);
				 Parameter p14 = new Parameter(12, String.format("/headImage/%d.jpg", random.nextInt(500) + 1), ParameterType.Input);

				 Object obj2 = Integer.valueOf(SqlHelperwrite.execute(cmdText2, new Parameter[] { p10, p11, p12, p13, p14 }));

				 if (obj2.equals(1)) {
					 LogManager.getInstance().Log("注册成功=="+obj2.toString());
					 return 101;
				 }else {
					 LogManager.getInstance().Log("注册错误");
					 return 1;
				 }

				}
		  return 3;

	}

	public static int ResetPassword(String acc, String pass, String yzm) {
		LogManager.getInstance().Log("1修改密码："+acc);
		LogManager.getInstance().Log("2修改密码："+pass);
		LogManager.getInstance().Log("3修改密码："+yzm);
		 String sql = "UPDATE users SET password = ? WHERE account = ?";
		 Parameter account = new Parameter(Types.VARCHAR, acc, ParameterType.Input);
		 Parameter password = new Parameter(Types.VARCHAR, pass, ParameterType.Input);

		Object obj2 = Integer.valueOf(SqlHelperwrite.execute(sql, new Parameter[] { password, account }));
		if (obj2.equals(1)) {
			LogManager.getInstance().Log("修改成功=="+obj2.toString());
			return 101;
		}else {
			LogManager.getInstance().Log("修改错误");
			return 1;
		}

	}

	
}
